package cn.dglydrpy.study.algorithm.od;

import java.util.PriorityQueue;
import java.util.Scanner;

/**
 * @author Yyy
 * @date 2024/11/6 11:42
 * @description
 * 题目描述：公司要选拔 10 名员工参加拔河比赛，选拔规则是按身高优先、体重次之的方式，需编写程序实现。
 * 输入是记录员工身高和体重信息的数组，输出为 10 行的二维数组，每行表示一名参赛员工的身高和体重。
 *
输入：
20
160 161 162 165 168 172 173 169 176 179 180 181 182 183 184 177 179 165 172 159
70 67 68 72 73 90 87 85 88 77 91 93 95 97 92 89 86 79 78 76

输出：
184 92
183 97
182 95
181 93
180 91
179 86
179 77
177 89
176 88
173 87
 */
public class Od0042_HeightWeightSortTop10 {

    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);
        while(scanner.hasNextLine()){
            int n = Integer.valueOf(scanner.nextLine());
            String[] heights = scanner.nextLine().split(" ");
            String[] weights = scanner.nextLine().split(" ");

            Emp[] emps = new Emp[n];
            for(int i=0;i<n;i++){
                Emp emp = new Emp(Integer.valueOf(heights[i]),Integer.valueOf(weights[i]));
                emps[i] = emp;
            }

            System.out.println(myAnswer(emps));
        }
        
    }

    private static String myAnswer(Emp[] emps) {

        StringBuilder ans = new StringBuilder();

        // 小根堆
        PriorityQueue<Emp> queue = new PriorityQueue<>(
                (o1,o2) ->{
                    return o1.height == o2.height ?
                             o1.weight - o2.weight
                            : o1.height - o2.height;
                }
        );
        
        for(Emp e : emps){
            if(queue.size() < 10){
                queue.add(e);
            }else{
                Emp peek = queue.peek();
                if(e.height > peek.height || e.weight > peek.weight){
                    queue.poll();
                    queue.add(e);
                }
            }
        }


        PriorityQueue<Emp> queue2 = new PriorityQueue<>(
                (o1,o2) ->{
                    return o2.height == o1.height ?
                            o2.weight - o1.weight
                            : o2.height - o1.height;
                }
        );
        
        while(!queue.isEmpty()){
            Emp emp = queue.poll();
            queue2.add(emp);
            
        }

        while(!queue2.isEmpty()){
            Emp emp = queue2.poll();
            ans.append(emp.height + " " + emp.weight + "\n");
        }
        return ans.toString();
        
    }

    public static class Emp {
        
        private int height;
        private int weight;

        public Emp(int height, int weight) {
            this.height = height;
            this.weight = weight;
        }
    }
}
